package com.tengju.user.domain.model.user;

import com.tengju.user.domain.model.club.ClubId;
import com.tengju.user.domain.shared.DomainException;
import com.tengju.user.domain.shared.DomainExceptionCode;
import lombok.Value;
import lombok.experimental.NonFinal;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

@Value
public class UserDetail {

    static final String INTERNAL_AREA_CODE = "+86";

    UserIdCode idCode;

    String areaCode;

    String mobile;

    StarLevelEnum starLevel;

    @NonFinal
    String name;

    @NonFinal
    String nickname;

    LocalDateTime createTime;

    UserIdCode parentIdCode;

    StarLevelEnum parentStarLevel;

    UserIdCode belongEc;

    UserIdCode belongSec;

    UserIdCode belongEec;

    ClubId belongClubId;

    LocalDateTime upgradeBlackCardTime;

    @NonFinal
    String wholePhoneNumber;

    public String wholePhoneNumber() {
        if (!StringUtils.isEmpty(mobile) && !StringUtils.isEmpty(areaCode)) {
            if (INTERNAL_AREA_CODE.equals(areaCode)) {
                return mobile;
            } else {
                return areaCode + mobile;
            }
        } else if (!StringUtils.isEmpty(wholePhoneNumber)) {
            return wholePhoneNumber;
        } else {
            return "";
        }
    }

    public void fillDealerInfo(UserInfo dealInfo) {
        if (starLevel != StarLevelEnum.NONE) {
            throw DomainException.error(DomainExceptionCode.INVALID_DATA,
                    "idCode:" + idCode.getValue() + ",该用户不是经销商");
        }
        if (dealInfo == null) {
            throw DomainException.error(DomainExceptionCode.INVALID_DATA,
                    "idCode:" + idCode.getValue() + ",未找到该用户");
        }

        this.wholePhoneNumber = dealInfo.getWholePhoneNumber();
        this.name = dealInfo.getName();
        this.nickname = dealInfo.getNickName();
    }

}
